package com.wuyoucar.service.impl;

import com.wuyoucar.common.ResultBean;
import com.wuyoucar.entity.TPolicy;
import com.wuyoucar.entity.TReal;
import com.wuyoucar.mapper.TPolicyMapper;
import com.wuyoucar.service.InTPolicyService;
import com.wuyoucar.util.CardTextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: jiangdz
 * @ProjectName: mywuyou_car
 * @Package: com.wuyoucar.service.imp
 * @ClassName: TPolicyServiceImpl
 * @Description:
 * @Date: 2020-09-24  11:46
 * @UpdateUser: asus
 * @UpdateDate: 2020-09-24  11:46
 * @UpdateRemark: [说明本次修改内容]
 * @Version: [v1.0]
 * @Since JDK 1.8
 */
@Service
public class TPolicyServiceImpl implements InTPolicyService {

    @Autowired
    TPolicyMapper tPolicyMapper;


    //查看保单
    @Override
    public ResultBean getPolicy(Integer userid) {
        ResultBean resultBean = new ResultBean();
        if (tPolicyMapper.queryById(userid).size() != 0) {
            resultBean.setCode(0);
            resultBean.setMsg("查询成功！");
            resultBean.setData(tPolicyMapper.queryById(userid));
        } else {
            resultBean.setCode(101);
            resultBean.setMsg("查询失败！");
        }
        return resultBean;
    }

    //加保
    @Override
    public ResultBean addPolicy(Integer policyno, Integer year) {
        ResultBean resultBean = new ResultBean();
        if (year == null) {
            resultBean.setMsg("请输入加保年限！");
            resultBean.setCode(-2);
        } else {
            //先查询到保单表的数据
            TPolicy tPolicy = tPolicyMapper.queryPolicyno(policyno);
            TPolicy policy = new TPolicy();
            //判断是否过期，如果是0 已过期，如果是1 正在生效 ，到期时间加上输入的值年
            if (tPolicy.getSafestate() == 0) {
                resultBean.setMsg("您的保单已失效，请重新下单！");
                resultBean.setCode(-1);
            } else {
                //取到保单表里的时间
                Date date = tPolicy.getExpiretime();
                //获取当前时间
                Date date1 = new Date();
                policy.setPolicyno(tPolicy.getPolicyno());
                policy.setLastmodifytime(date1);
                Calendar cal = Calendar.getInstance();
                //把日期往后增加,整数  往后推,负数往前移动
                cal.setTime(date);
                cal.add(Calendar.YEAR, year);
                //这个时间就是日期往后推的结果
                date = cal.getTime();
                policy.setExpiretime(date);
                int n = tPolicyMapper.updateTPolicyById(policy);
                if (n > 0) {
                    resultBean.setMsg("加保成功！");
                    resultBean.setCode(0);
                } else {
                    resultBean.setMsg("加保失败！");
                    resultBean.setCode(404);

                }
            }
        }

        return resultBean;
    }

    @Override
    public ResultBean selectPolicyno(Integer policyno, Integer userid) {
        ResultBean resultBean = new ResultBean();
        if (tPolicyMapper.selectPolicyno(policyno, userid) != null) {
            resultBean.setCode(0);
            resultBean.setMsg("保单号存在！");
            resultBean.setData(tPolicyMapper.selectPolicyno(policyno, userid));
        } else {
            resultBean.setCode(101);
            resultBean.setMsg("保单号不存在！");
        }
        System.out.println("tPolicyMapper.selectPolicyno(policyno,userid) = " + tPolicyMapper.selectPolicyno(policyno, userid));
        return resultBean;
    }


}
